package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 菜品控制器
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品入参{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        redisTemplate.delete("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分类查询")
    public Result<PageResult> page(DishPageQueryDTO queryDTO){
        log.info("菜品管理分页查询入参{}",queryDTO);
        PageResult pageResult=dishService.pageQuery(queryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public  Result delete(@RequestParam List<Long> ids){ //这里前端传过来的是id字符串，例如  1，2，3，使用注解 RequestParam ，springMVC会自动将一个id字符串拆分成一个个id
        log.info("菜品批量删除入参{}",ids);

        //根据菜品id，查询他们的分类id组成集合，然后循环删除缓存
        List<Long> categoryIdList = ids.stream()
                .map(id -> dishService.getCategoryByDishId(id))
                .collect(Collectors.toList());
        String key="dish_";
        dishService.deleteBatch(ids);
        if(categoryIdList!=null && categoryIdList.size()>0){
            log.info("缓存中将要删除的菜品分类有：{}",categoryIdList);
            for (Long caId : categoryIdList) {
                redisTemplate.delete(key+caId.toString());

            }
        }
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品入参{}",id);
        DishVO dishVO=dishService.getById(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("菜品修改")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品入参{}",dishDTO);
        //菜品的修改有两种情况，一种是不修改菜品分类的，这种只要删除redis中，该分类下，所有数据
        //  二是修改了菜品所属分类的，这种要删除修改前，修改后，两个分类的所有数据
        List<Long> categoryidList = new ArrayList<>();
        categoryidList.add(dishDTO.getCategoryId());
        // 根据dishid，获取修改前该菜品的分类id
        DishVO dishVO = dishService.getById(dishDTO.getId());
        categoryidList.add(dishVO.getCategoryId());

        if (categoryidList!=null && categoryidList.size()>0){
            for (Long categoryId : categoryidList) {
                redisTemplate.delete("dish_"+categoryId);
            }
        }

        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){

        DishVO dishVO = dishService.getById(id);
        redisTemplate.delete("dish_"+dishVO.getCategoryId());

        dishService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

}
